import 'package:flutter/material.dart';
import '../../features/main/pages/main_page.dart';
import '../../features/splash/pages/splash_page.dart';
import '../../features/auth/pages/auth_page.dart';
import '../../features/common/pages/error_page.dart';
import '../../features/learning/pages/learning_hub_page.dart';
import '../../features/learning/pages/media_presentation_screen.dart';
import '../../features/user_detail/pages/other_user_page.dart';
import '../models/detail_arguments.dart';
import '../models/teach.dart';

/// 应用路由管理器
class AppRouter {
  /// 路由名称常量
  static const String splash = '/';
  static const String main = '/main';
  static const String home = '/home';
  static const String login = '/login';
  static const String detail = '/detail';
  static const String learningHub = '/learning-hub';
  static const String mediaPresentation = '/media-presentation';
  static const String otherUser = '/other-user';

  /// 生成路由
  static Route<dynamic> generateRoute(RouteSettings settings) {
    switch (settings.name) {
      case splash:
        return _createRoute(const SplashPage(), settings);
      case main:
        return _createRoute(const MainPage(), settings);
      case login:
        return _createRoute(const AuthPage(), settings);
      case learningHub:
        // 支持传递初始tab参数
        final initialTab = settings.arguments as String?;
        return _createRoute(LearningHubPage(initialTab: initialTab), settings);
      case mediaPresentation:
        // 支持传递教学内容参数
        final teachContent = settings.arguments as Teach?;
        if (teachContent == null) {
          return _createRoute(const ErrorPage(message: 'error'), settings);
        }
        return _createRoute(
          MediaPresentationScreen(teachContent: teachContent),
          settings,
        );
      case otherUser:
        // 支持传递用户ID参数
        final userId = settings.arguments as String?;
        if (userId == null) {
          return _createRoute(
            const ErrorPage(message: 'User ID required'),
            settings,
          );
        }
        return _createRoute(OtherUserPage(userId: userId), settings);
      default:
        return _createRoute(const NotFoundPage(), settings);
    }
  }

  /// 创建带动画的路由
  static PageRouteBuilder _createRoute(Widget page, RouteSettings settings) {
    return PageRouteBuilder(
      settings: settings,
      pageBuilder: (context, animation, secondaryAnimation) => page,
      transitionDuration: const Duration(milliseconds: 300),
      reverseTransitionDuration: const Duration(milliseconds: 300),
      transitionsBuilder: (context, animation, secondaryAnimation, child) {
        // 根据路由名称选择不同的动画效果
        switch (settings.name) {
          case splash:
            return _fadeTransition(animation, child);
          case main:
            return _slideTransition(animation, child, SlideDirection.right);
          case login:
            return _slideAndFadeTransition(animation, child, SlideDirection.up);
          default:
            return _fadeTransition(animation, child);
        }
      },
    );
  }

  /// 淡入淡出动画
  static Widget _fadeTransition(Animation<double> animation, Widget child) {
    return FadeTransition(opacity: animation, child: child);
  }

  /// 滑动动画
  static Widget _slideTransition(
    Animation<double> animation,
    Widget child,
    SlideDirection direction,
  ) {
    Offset begin;
    const Offset end = Offset.zero;

    switch (direction) {
      case SlideDirection.left:
        begin = const Offset(-1.0, 0.0);
        break;
      case SlideDirection.right:
        begin = const Offset(1.0, 0.0);
        break;
      case SlideDirection.up:
        begin = const Offset(0.0, 1.0);
        break;
      case SlideDirection.down:
        begin = const Offset(0.0, -1.0);
        break;
    }

    final tween = Tween(begin: begin, end: end);
    final offsetAnimation = animation.drive(tween);

    return SlideTransition(position: offsetAnimation, child: child);
  }

  /// 组合动画（滑动 + 淡入）
  static Widget _slideAndFadeTransition(
    Animation<double> animation,
    Widget child,
    SlideDirection direction,
  ) {
    return SlideTransition(
      position: _getSlideAnimation(animation, direction),
      child: FadeTransition(opacity: animation, child: child),
    );
  }

  /// 获取滑动动画
  static Animation<Offset> _getSlideAnimation(
    Animation<double> animation,
    SlideDirection direction,
  ) {
    Offset begin;
    const Offset end = Offset.zero;

    switch (direction) {
      case SlideDirection.left:
        begin = const Offset(-1.0, 0.0);
        break;
      case SlideDirection.right:
        begin = const Offset(1.0, 0.0);
        break;
      case SlideDirection.up:
        begin = const Offset(0.0, 1.0);
        break;
      case SlideDirection.down:
        begin = const Offset(0.0, -1.0);
        break;
    }

    return Tween<Offset>(
      begin: begin,
      end: end,
    ).animate(CurvedAnimation(parent: animation, curve: Curves.easeInOut));
  }
}

/// 滑动方向枚举
enum SlideDirection { left, right, up, down }

/// 404页面
class NotFoundPage extends StatelessWidget {
  const NotFoundPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Page not found')),
      body: const Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Icon(Icons.error_outline, size: 64, color: Colors.grey),
            SizedBox(height: 16),
            Text(
              '404',
              style: TextStyle(
                fontSize: 48,
                fontWeight: FontWeight.bold,
                color: Colors.grey,
              ),
            ),
            SizedBox(height: 8),
            Text(
              'Page not found',
              style: TextStyle(fontSize: 16, color: Colors.grey),
            ),
          ],
        ),
      ),
    );
  }
}
